home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Source Code / C / Frameworks / Recursive Shell 1.0.1 / Examples / ListFiles ƒ / DoStuff.c < prev    next >
Text File  |  1996-06-13  |  6KB  |  245 lines

  1. /********************************************************************************/
  2. //
  3. //    DoStuff.c
  4. //
  5. //    Part of a shell program, this is the file that will be changed to make it do
  6. //    what it's suppossed to do.
  7. //
  8. /********************************************************************************/
  9.  
  10.  
  11. #include    "DialogUtil.h"
  12. #include    "DoStuff.h"
  13. #include    "Utility.h"
  14.  
  15.  
  16. //    Global variables
  17.  
  18. short        gLFileRef;
  19.  
  20.  
  21. //    Prototypes
  22. void    AppendNameToFile( FSSpec theFile );
  23. void    CopyNameToPtr( Str63 name, Ptr namePtr );
  24.  
  25.  
  26. //    Routines that will be called by the other parts of this program:
  27. //    (Each of these must be present, and their names must be unchanged)
  28. //
  29. //    InitializeStuff
  30. //    DoToEachFile
  31. //    DoToEachFolder
  32. //    DoToSelFolder
  33. //    DeInitializeStuff
  34.  
  35.  
  36. /********************************************************************************/
  37. //
  38. //    InitializeStuff is a routine which gets called before anything else happens.
  39. //    This allows the opportunity to set global variables, read preferences, etc.
  40. //    that will affect how the other parts of this file behave.
  41. //    
  42. //    Expected to return true if initialization succeeded and we should continue
  43. //    processing files.
  44. //
  45. /********************************************************************************/
  46.  
  47. Boolean    InitializeStuff( void )
  48. {
  49. //    Get the name of a text file from the user and create or open it.
  50. //    Store the reference to it in a global variable for later use.
  51.  
  52.     StandardFileReply    reply;
  53.     OSErr                err;
  54.     Str255                returnValue;
  55.     
  56.     StandardPutFile ( "\pSave results to what file?", "\pUntitled", &reply );
  57.     
  58.     if ( reply.sfGood )    //    User did not hit cancel.
  59.     {
  60.         if ( !( reply.sfReplacing ) )    //    User gave a new file name.
  61.         {
  62.             //    Create a new file:
  63.             err = FSpCreate( &reply.sfFile, 'R*ch', 'TEXT', reply.sfScript );
  64.             if ( err != noErr )
  65.             {
  66.                 ResolveOSErr( err, returnValue );
  67.                 DisplayAlert( returnValue );
  68.                 return ( false );
  69.             }
  70.         }
  71.         
  72.         //    Open file with write access:
  73.         err = FSpOpenDF( &reply.sfFile, fsWrPerm, &gLFileRef );
  74.         if ( err != noErr )
  75.         {
  76.             ResolveOSErr( err, returnValue );
  77.             DisplayAlert( returnValue );
  78.             return ( false );
  79.         }
  80.     }
  81.     else    //    User did hit cancel
  82.         return ( false );
  83.     
  84.     return ( true );
  85. }
  86.  
  87.  
  88. /********************************************************************************/
  89. //
  90. //    DoToEachFile is a routine which is called for each file the recursion routine
  91. //    encounters.
  92. //
  93. /********************************************************************************/
  94.  
  95. void    DoToEachFile( FSSpec fileSpec )
  96. {
  97.     AppendNameToFile( fileSpec );
  98. }
  99.  
  100.  
  101. /********************************************************************************/
  102. //
  103. //    DoToEachFolder is a routine which is called for each file the recursion
  104. //    routine encounters.
  105. //
  106. /********************************************************************************/
  107.  
  108. void    DoToEachFolder( FSSpec folderSpec )
  109. {
  110.     AppendNameToFile( folderSpec );
  111. }
  112.  
  113.  
  114. /********************************************************************************/
  115. //
  116. //    If the user dropped a file on to the icon, after all calls to DoToEachFile
  117. //    and DoToEachFolder are completed, DoToSelFile is called with a reference to
  118. //    the file the user dropped.
  119. //
  120. /********************************************************************************/
  121.  
  122. void    DoToSelFile( FSSpec    fileSpec )
  123. {
  124.     AppendNameToFile( fileSpec );
  125. }
  126.  
  127.  
  128. /********************************************************************************/
  129. //
  130. //    If the user selected a folder or dropped one on to the icon, after all calls
  131. //    to DoToEachFile and DoToEachFolder are completed, DoToSelFolder is called with
  132. //    a reference to the folder the user selected or dropped.
  133. //
  134. /********************************************************************************/
  135.  
  136. void    DoToSelFolder( FSSpec folderSpec )
  137. {
  138.     AppendNameToFile( folderSpec );
  139. }
  140.  
  141.  
  142.  
  143. /********************************************************************************/
  144. //
  145. //    DeInitializeStuff is a routine which is called after all the recursive stuff
  146. //    is done.  It allows us to close up any files opened, release memory, etc.
  147. //
  148. /********************************************************************************/
  149.  
  150. void    DeInitializeStuff( void )
  151. {
  152.     OSErr    err;
  153.     long    filePos;
  154.     Str255    returnValue;
  155.     
  156.     //    Truncate file where we finished writing:
  157.     err = GetFPos( gLFileRef, &filePos );
  158.     if ( err != noErr )
  159.     {
  160.         ResolveOSErr( err, returnValue );
  161.         DisplayAlert( returnValue );
  162.     }
  163.     else
  164.     {
  165.         err = SetEOF( gLFileRef, filePos );
  166.         if ( err != noErr )
  167.         {
  168.             ResolveOSErr( err, returnValue );
  169.             DisplayAlert( returnValue );
  170.         }
  171.     }
  172.  
  173.     //    Close file:
  174.     err = FSClose( gLFileRef );
  175.     if ( err != noErr )
  176.     {
  177.         ResolveOSErr( err, returnValue );
  178.         DisplayAlert( returnValue );
  179.     }
  180. }
  181.  
  182.  
  183. /********************************************************************************/
  184. //
  185. //    Misc. routines to support what this app needs to do go below here
  186. //
  187. /********************************************************************************/
  188.  
  189. void    AppendNameToFile( FSSpec fileSpec )
  190. {
  191.     Str255    returnValue, fullPathName;
  192.     long    count, origCount;
  193.     Ptr        namePtr;
  194.     OSErr    err;
  195.     
  196.     GetFullPath( &fileSpec, fullPathName );
  197.     
  198.     count = fullPathName[0] + 1;    //    Add one for the return character
  199.     origCount = count;
  200.     
  201.     fullPathName[0] = count;        //    Extend string by one characer
  202.     fullPathName[count] = '\n';        //    Add the return
  203.     
  204.     namePtr = NewPtr( count );        //    Try to allocate some memory
  205.     if ( namePtr == nil )
  206.         DisplayAlert( "\pERROR: Out of memory!" );
  207.     else
  208.     {
  209.         CopyNameToPtr( fullPathName, namePtr );
  210.         
  211.         err = FSWrite( gLFileRef, &count, namePtr );
  212.         if ( err != noErr )
  213.         {
  214.             ResolveOSErr( err, returnValue );
  215.             DisplayAlert( returnValue );
  216.         }
  217.         else
  218.         {
  219.             if ( count != origCount )
  220.                 DisplayAlert( "\pWARNING: Incomplete data written to file (full disk?)" );
  221.         }    //    FSWrite error check
  222.         
  223.         DisposePtr( namePtr );
  224.         
  225.     }    //    NewPtr error check
  226. }
  227.  
  228.  
  229. void    CopyNameToPtr( Str255 name, Ptr namePtr )
  230. {
  231.     short    i;
  232.     int        length;
  233.     
  234.     length = *name++;    //    Get length of the name + move past length byte
  235.     
  236.     for ( i = 0; i < length; i++ )
  237.         *namePtr++ = *name++;
  238.     
  239.     *namePtr++ = '\n';    //    Add a return character at the end
  240. }
  241.  
  242.  
  243.  
  244.  
  245.